'''
Created on 09/04/2013

@author: tsantana
'''

from thilux_models import Department, Commision, Employee, Menu, MenuItem, Permission, Position, Product, Role, Sale, SaleItem, SalesPerson, User
from datetime import datetime
import thilux_security

def clean_data():
    pass
    
def populate_data():
    '''
        Creating Permissions
    '''
    app_admin_permission=Permission(key_name='AppAdmin')
    app_admin_permission.put()
    dept_manager_permission=Permission(key_name='Manager')
    dept_manager_permission.put()
    basic_employee_permission=Permission(key_name='BasicEmployee')
    basic_employee_permission.put()
    sales_person_permission=Permission(key_name='SalesPerson')
    sales_person_permission.put()
    
    '''
        Creating Roles
    '''
    
    app_admin_role=Role(key_name='Application Admin')
    app_admin_role.permission=app_admin_permission
    app_admin_role.status=1
    app_admin_role.put()
    
    dept_manager_role=Role(key_name='Department Manager')
    dept_manager_role.permission=dept_manager_permission
    dept_manager_role.status=1
    dept_manager_role.put()
    
    employee_role=Role(key_name='Employee')
    employee_role.permission=basic_employee_permission
    employee_role.status=1
    employee_role.put()
    
    sales_person_role=Role(key_name='Sales Person')
    sales_person_role.permission=sales_person_permission
    sales_person_role.status=1
    sales_person_role.put()
    
    '''
        Creating menus
    '''
    admin_menu=Menu()
    admin_menu.title='Administrative Tasks'
    admin_menu.role=app_admin_role
    admin_menu.put()
    
    manager_menu_1=Menu()
    manager_menu_1.title='Administrative Tasks'
    manager_menu_1.role=dept_manager_role
    manager_menu_1.put()
    
    manager_menu_2=Menu()
    manager_menu_2.title='Sales Administration'
    manager_menu_2.role=dept_manager_role
    manager_menu_2.put()
    
    manager_menu_3=Menu()
    manager_menu_3.title='Reports'
    manager_menu_3.role=dept_manager_role
    manager_menu_3.put()
    
    menu_item=MenuItem()
    menu_item.menu=admin_menu
    menu_item.name='Users'
    menu_item.link='/users'
    menu_item.put()
    
    menu_item=MenuItem()
    menu_item.menu=admin_menu
    menu_item.name='Departments'
    menu_item.link='/departments'
    menu_item.put()
    
    menu_item=MenuItem()
    menu_item.menu=admin_menu
    menu_item.name='Products'
    menu_item.link='/products'
    menu_item.put()
    
    menu_item=MenuItem()
    menu_item.menu=admin_menu
    menu_item.name='Sales'
    menu_item.link='/sales'
    menu_item.put()
    
    menu_item=MenuItem()
    menu_item.menu=manager_menu_1
    menu_item.name='Employees'
    menu_item.link='/employees'
    menu_item.put()
    
    menu_item=MenuItem()
    menu_item.menu=manager_menu_1
    menu_item.name='Products'
    menu_item.link='/products'
    menu_item.put()
    
    menu_item=MenuItem()
    menu_item.menu=manager_menu_1
    menu_item.name='Sales'
    menu_item.link='/sales'
    menu_item.put()
    
    menu_item=MenuItem()
    menu_item.menu=manager_menu_2
    menu_item.name='Commissions'
    menu_item.link='/commissions'
    menu_item.put()
    
    menu_item=MenuItem()
    menu_item.menu=manager_menu_2
    menu_item.name='Sales Personel'
    menu_item.link='/salespersonel'
    menu_item.put()
    
    menu_item=MenuItem()
    menu_item.menu=manager_menu_3
    menu_item.name='Sales Reports'
    menu_item.link='/salesreports'
    menu_item.put()
    
    menu_item=MenuItem()
    menu_item.menu=manager_menu_3
    menu_item.name='Commissions Reports'
    menu_item.link='/commissionsreports'
    menu_item.put()
    
    menu_item=MenuItem()
    menu_item.menu=manager_menu_3
    menu_item.name='Stock Reports'
    menu_item.link='/stockreports'
    menu_item.put()
    
def create_admin(login, password):
    user=User(key_name=login)
    user.first_name='Admin'
    user.last_name=login
    user.status=1
    user.birth_date=datetime.today().date()
    user.password=thilux_security.encrypt(password)
    
    role = Role.get_by_key_name('Application Admin')
    
    user.role=role